package com.joy.ogljoy_01.opengl;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Graphics {
	public static final int TOP             = 1<<1;
    public static final int BOTTOM          = 1<<2;
    public static final int LEFT            = 1<<3;
    public static final int RIGHT           = 1<<4;
    public static final int VCENTER         = 1<<5;
    public static final int HCENTER         = 1<<6;
	public static final int LEFT_TOP        = LEFT | TOP;
    public static final int LEFT_BOTTOM     = LEFT | BOTTOM;
    public static final int LEFT_VCENTER    = LEFT | VCENTER;
    public static final int LEFT_HCENTER    = LEFT | HCENTER;
	public static final int RIGHT_TOP       = RIGHT | TOP;
    public static final int RIGHT_BOTTOM    = RIGHT | BOTTOM;
    public static final int RIGHT_VCENTER   = RIGHT | VCENTER;
    public static final int RIGHT_HCENTER   = RIGHT | HCENTER;
    
    public static final int TRANS_NONE = 0;
    public static final int TRANS_ROT90 = 5;
    public static final int TRANS_ROT180 = 3;
    public static final int TRANS_ROT270 = 6;
    public static final int TRANS_MIRROR = 2;
    public static final int TRANS_MIRROR_ROT90 = 7;
    public static final int TRANS_MIRROR_ROT180 = 1;
    public static final int TRANS_MIRROR_ROT270 = 4;
    

    public static final byte BLEND_NORMAL = 1;
    public static final byte BLEND_SCREEN = 2;
    public static final byte BLEND_MASK = 3;
    public static final byte BLEND_CLIP = 4;
    public static final byte BLEND_VEIN = 5;

	public static GL10 gl;
	
	private static RenderBuffer buffer = new RenderBuffer(1);
	
	/** 只支持绘制一个节点 */
	public static void draw(int texId, float[] vertexs, float[] texcoords) {
		
		buffer.clear();
		buffer.put(vertexs, texcoords);
		
		draw(texId, buffer.getVertexs(), buffer.getTexcoords());
	}
	
	public static void draw(int texId, float[] vertexs, FloatBuffer texcoords) {
		
		buffer.clear();
		buffer.put(vertexs, texcoords);
		
		draw(texId, buffer.getVertexs(), buffer.getTexcoords());
	}
	
	public static void draw(int texId, RenderBuffer buffer) {
		draw(texId, buffer.getVertexs(), buffer.getTexcoords(), buffer.getIndexs());
	}
	
	/** 支持绘制2D节点集合 */
	public static void draw(int texId, FloatBuffer vertexs, FloatBuffer texcoords, ShortBuffer indexBuffer) {
		bindBuffer(texId, vertexs, texcoords);
		indexBuffer.position(0);
		gl.glDrawElements(GL10.GL_TRIANGLES, indexBuffer.remaining(), GL10.GL_UNSIGNED_SHORT, indexBuffer);
	}
	
	public static void draw(int texId, FloatBuffer vertexs, FloatBuffer texcoords) {
		bindBuffer(texId, vertexs, texcoords);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
	}

	private static void bindBuffer(int texId, FloatBuffer vertexs, FloatBuffer texcoords) {
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texId);
		vertexs.position(0);
		gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexs);
		texcoords.position(0);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texcoords);
	}
}
